package gui;

import java.util.ArrayList;

import org.newdawn.slick.Graphics;

public class ButtonMenu {

	private int selected_button_index;
  private int buttonHeight;
	private boolean canRotate;

	private ArrayList<Button> list;
	
	//***********************************
	// CONSTRUCTOR
	public ButtonMenu(){
		canRotate = false;
		list = new ArrayList<Button>();
		this.buttonHeight = 18;
	}
	
	//***********************************
	// Methods
	/**
	 * Appends the specified button to the end of this menu
	 * @param b button to be appended to this menu
	 * @return 
	 */
	public void add(Button b)
	{
		this.list.add(b); //list.add always return true, because ArrayList accept duplicates entries
	}
	
	public void add(int index, Button b)
	{
		this.list.add(index, b);
	}
	
	/**
	 * Removes the first occurrence of the specified button from this menu, if it is present. 
	 * If the menu does not contain the element, it is unchanged. More formally, removes the 
	 * button with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) 
	 * (if such an button exists). Returns true if this menu contained the specified button 
	 * (or equivalently, if this list changed as a result of the call). 
	 * 
	 * @param b button to be removed from this menu, if present 
	 * @return true if this menu contained the specified button
	 */
	public boolean remove(Button b)
	{
		return this.list.remove(b);
	}
	
	/**
	 * Removes the button at the specified position in this list. Shifts any subsequent buttons 
	 * to the left (subtracts one from their indices). 
	 * 
	 * @param index the index of the button to be removed 
	 * @return the button that was removed from the list 
	 */
	public Button remove(int index)
	{
		return this.list.remove(index);
	}
	
	public void clear()
	{
		this.list.clear();
	}
	
	public int size()
	{
		return this.list.size();
	}
	
	public Button get(int index)
	{
		return this.list.get(index);
	}
	
	/**
	 * Returns the index of the first occurrence of the specified button in this menu, 
	 * or -1 if this list does not contain the element. 
	 * More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))),
	 * or -1 if there is no such index. 
	 * @param b
	 * @return
	 */
	public int indexOf(Button b)
	{
		return list.indexOf(b);
	}
	
	public void draw(int x, int y, Graphics g){
		
		Button b;
		
		for (int i = 0; i < this.size(); ++i) 
		{   
			b = this.get(i);
			b.draw(x, y+i*buttonHeight, g);
    }


	}
	
	public boolean next()
	{
		if(selected_button_index >= this.getLastIndex())
		{
			if(canRotate)
				this.setSelected(0);
			else
				return false;
		}
		else
		{
			this.setSelected(selected_button_index + 1);
		}
		
		return true;
	}
	
	public boolean prev()
	{
		if(selected_button_index <= 0)
		{
			if(canRotate)
				this.setSelected(this.getLastIndex());
			else 
				return false;
		}
		else
		{
			this.setSelected(selected_button_index - 1);
		}
		
		return true;
	}
	
	//**************************************************************
	// GETTERS / SETTERS
	public Button getSelected()
	{
		return this.get(this.selected_button_index);
	}
	
	public boolean setSelected(int index)
	{
		if(index >= this.size())
			return false;
		
		//unselect and select button in menu
		this.get(this.selected_button_index).setSelected(false);
		this.get(this.selected_button_index = index).setSelected(true);
				
		return true;
	}
	
	public boolean canRotate()
	{
		return canRotate();
	}
	
	public void setCanRotate(boolean canRotate)
	{
		this.canRotate = canRotate;
	}
	
	public int getLastIndex()
	{
		return this.size() - 1;
	}
	
}
